/**
 * Copyright (c) 2016-2019 九点科技 All rights reserved.
 *
 * http://www.9dyun.cn
 *
 * 版权所有，侵权必究！
 */
package com.dian.modules.sale.service.impl;

import com.dian.common.utils.*;
import com.dian.common.validator.ValidatorUtils;
import com.dian.common.validator.group.AddGroup;
import com.dian.common.validator.group.UpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import cn.hutool.core.util.StrUtil;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import com.dian.common.server.CommonService;
import com.dian.modules.sale.dao.InstallFinishDao;
import com.dian.modules.sale.entity.InstallFinishEntity;
import com.dian.modules.sale.service.InstallFinishService;
import com.dian.modules.sale.vo.InstallFinishExportVO;

/**
 * 安装完工单服务实现类
 *
 * @author xiaolai
 * @email xiaolai@9dyun.cn
 * @date 2019-06-21 15:24:30
 */
@Service("installFinishService")
public class InstallFinishServiceImpl extends ServiceImpl<InstallFinishDao, InstallFinishEntity> implements InstallFinishService {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    public CommonService commonService;


    /**
     * 安装完工单分页
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        IPage<InstallFinishEntity> page = this.page(new Query<InstallFinishEntity>().getPage(params),getQueryWrapper(params) );
        return new PageUtils(page);
    }

    /**
     *  安装完工单新增
     * @param installFinishEntity
     * @return
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public boolean saveInfo(InstallFinishEntity installFinishEntity) {

        //设置编码，等基础默然初始化数据设置
        //数据完整性校验
        this.paramsCheck(installFinishEntity,AddGroup.class);

        //保存
        this.save(installFinishEntity);

        return true;
    }

    /**
     *安装完工单更新
     * @param installFinishEntity
     * @return
     */
    @Override
    public boolean updateInfo(InstallFinishEntity installFinishEntity) {

        //修改状态校验
        this.updateCheck(installFinishEntity.getId());

        //主表数据完整性校验
        this.paramsCheck(installFinishEntity, UpdateGroup.class);

        //更新主表
        this.updateById(installFinishEntity);


        return true;
    }

    /**
     *安装完工单删除
     * @param ids
     * @return
     */
    @Override
    public boolean deleteInfo(Long[] ids) {

        //删除状态校验
        this.deleteCheck(ids);

        //更新主表
        this.remove(new QueryWrapper<InstallFinishEntity>().in("id",ids));

        return true;
    }

    /**
     * 安装完工单详情
     * @param id
     * @return
     */
    @Override
    public InstallFinishEntity getInfo(Long id) {
        InstallFinishEntity installFinishEntity =getById(id);
        return installFinishEntity;
    }

    /**
     * 安装完工单审核
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public boolean checkInfo(Long id) {
        InstallFinishEntity installFinishEntity =this.getById(id);
        checkCheck(installFinishEntity);
        //installFinishEntity.setOrderState(OrderHead_OrderStateEnum.AUDITED.getValue());
        //installFinishEntity.setOrderDate(new Date());
        ///installFinishEntity.setCheckUserId(commonService.getUserId());
        //installFinishEntity.setCheckUserName(commonService.getUserName());
        return this.updateById(installFinishEntity);
    }

    /**
     *安装完工单当前页or全部导出
     * @param params
     * @return
     */
    @Override
    public List<InstallFinishExportVO> exportList(Map<String, Object> params) {
        List<InstallFinishEntity> list;
        Integer exprotType= PUtil.getParaToInt(params.get("exprotType"),0);
        if(exprotType==0){
            list= this.page(new Query<InstallFinishEntity>().getPage(params),getQueryWrapper(params)).getRecords();
        }else{
            list= this.list(getQueryWrapper(params));
        }

        List<InstallFinishExportVO> resultList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(list)) {
            resultList = BeanConverter.convertList(list, InstallFinishExportVO.class);
        }
        return resultList;
    }



    /***********************************************************************************************/
    /****************************************** 私有方法 ********************************************/
    /***********************************************************************************************/

    /**
     * 修改状态校验
     *
     * @param
     */
    private void updateCheck(Long id) {
        InstallFinishEntity installFinishEntity =this.getById(id);
        //if(installFinishEntity.getOrderState().equals(OrderHead_OrderStateEnum.AUDITED.getValue())){
        //    throw new RRException(String.format("已审核的销售订单禁止修改"));
        //}
    }
    /**
     * 审核状态校验
     *
     * @param
     */
    private void checkCheck(InstallFinishEntity installFinishEntity) {
        //if(installFinishEntity.getOrderState().equals(OrderHead_OrderStateEnum.AUDITED.getValue())){
        //    throw new RRException(String.format("[%s] 此销售单已审核"));
        //}
    }

    /**
     * 删除状态校验
     *
     * @param ids
     */
    private void deleteCheck(Long[] ids) {
        for (Long id:ids) {
                InstallFinishEntity installFinishEntity = this.getById(id);
        }

    }

    /**
     * 新增和修改参数校验
     *
     * @param record
     */
    private void paramsCheck(InstallFinishEntity record, Class<?> cls) {
        ValidatorUtils.validateEntity(record, cls);
    }
    /**
     * 获取查询条件
     *
     * @param
     */
    private QueryWrapper<InstallFinishEntity> getQueryWrapper(Map<String, Object> params) {
        QueryWrapper<InstallFinishEntity> queryWrapper=new QueryWrapper<>();
        if(!StrUtil.isBlankIfStr(params.get("finishDate"))){
            String finishDate=String.valueOf(params.get("finishDate"));
            String[] finishDateRange=finishDate.split("至");
            if(finishDateRange.length==2){
                queryWrapper.gt("finish_date",DateUtils.getBeginTimeOfDate(finishDateRange[0]));
                queryWrapper.lt("finish_date",DateUtils.getBeginTimeOfDate(finishDateRange[1]));
            }else{
                queryWrapper.gt("finish_date",DateUtils.getBeginTimeOfDate(finishDate));
            }
        }
        if(!StrUtil.isBlankIfStr(params.get("probNo"))){
            queryWrapper.like("prob_no",params.get("probNo"));
        }
        if(!StrUtil.isBlankIfStr(params.get("orderNo"))){
            queryWrapper.like("order_no",params.get("orderNo"));
        }
        if(!StrUtil.isEmptyIfStr(params.get("propKey"))){
            queryWrapper.and(wrapper -> wrapper
            );
        }
        if(!StrUtil.isBlankIfStr(params.get("probId"))){
            queryWrapper.eq("prob_id",params.get("probId"));
        }
        queryWrapper.orderByAsc("id");
        return queryWrapper;
    }
}
